package org.sixtynine.stock.entity;

/**
 * @author Thilan
 * this class handle company data
 */

import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.Size;

import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.validator.constraints.NotEmpty;

/**
 * @author Thilan
 * 
 */
@Entity
@Table(name = "COMPANY")
public class Company extends BaseEntity {

	private int id;

	@Size(min = 3)
	@NotEmpty
	private String code;

	@NotEmpty
	private String name;
	private Sector sector;

	private Set<AnnualShareData> annualShareData;
	private Set<IntradayShareData> intradayShareData;
	private Set<QuarterlyShareData> quarterlyShareData;
	private Set<DailyShareData> dailyShareData;
	private Set<CompanyDocument> companyDoc;

	private Set<Dividend> dividend;

	private Set<SubSector> subSectors;

	private boolean largeCap;
	private boolean asi;
	private boolean snp;
	private boolean status;

	public Company() {
	}

	public Company(String name) {
		this.name = name;
	}

	public Company(String name, Set<SubSector> subSectors) {
		this.name = name;
		this.subSectors = subSectors;
	}

	@Id
	@Column(name = "ID", unique = true, nullable = false)
	@GeneratedValue(strategy = GenerationType.AUTO)
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Column(name = "CODE", unique = true, nullable = false, length = 30)
	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	@Column(name = "NAME", nullable = false, length = 30)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@ManyToOne
	@JoinColumn(name = "SECTOR_ID")
	public Sector getSector() {
		return sector;
	}

	public void setSector(Sector sector) {
		this.sector = sector;
	}

	@Override
	public boolean equals(Object obj) {
		return (id == ((Company) obj).getId());
	}

	@OneToMany(mappedBy = "company")
	public Set<AnnualShareData> getAnnualShareData() {
		return annualShareData;
	}

	public void setAnnualShareData(Set<AnnualShareData> annualShareData) {
		this.annualShareData = annualShareData;
	}

	@OneToMany(mappedBy = "company")
	public Set<IntradayShareData> getIntradayShareData() {
		return intradayShareData;
	}

	public void setIntradayShareData(Set<IntradayShareData> intradayShareData) {
		this.intradayShareData = intradayShareData;
	}

	@OneToMany(mappedBy = "company")
	public Set<QuarterlyShareData> getQuarterlyShareData() {
		return quarterlyShareData;
	}

	public void setQuarterlyShareData(Set<QuarterlyShareData> quarterlyShareData) {
		this.quarterlyShareData = quarterlyShareData;
	}

	@OneToMany(mappedBy = "company")
	public Set<DailyShareData> getDailyShareData() {
		return dailyShareData;
	}

	public void setDailyShareData(Set<DailyShareData> dailyShareData) {
		this.dailyShareData = dailyShareData;
	}

	@Column(name = "LARGE_CAP_COMPANY", nullable = false, length = 1)
	public boolean getLargeCap() {
		return largeCap;
	}

	public void setLargeCap(boolean largeCap) {
		this.largeCap = largeCap;
	}

	@Column(name = "ASI", nullable = false, length = 1)
	public boolean getAsi() {
		return asi;
	}

	public void setAsi(boolean asi) {
		this.asi = asi;
	}

	@Column(name = "SANDPP", nullable = false, length = 1)
	public boolean getSnp() {
		return snp;
	}

	public void setSnp(boolean snp) {
		this.snp = snp;
	}

	@Override
	public Object _getKey() {
		return code;
	}

	@OneToMany(mappedBy = "company")
	public Set<Dividend> getDividend() {
		return dividend;
	}

	public void setDividend(Set<Dividend> dividend) {
		this.dividend = dividend;
	}

	@OneToMany(fetch = FetchType.EAGER, mappedBy = "company")
	@Fetch(FetchMode.SELECT)
	@BatchSize(size = 10)
	public Set<CompanyDocument> getCompanyDoc() {
		return companyDoc;
	}

	public void setCompanyDoc(Set<CompanyDocument> companyDoc) {
		this.companyDoc = companyDoc;
	}

	@Column(name = "STATUS", nullable = false, length = 1)
	public boolean isStatus() {
		return status;
	}

	public void setStatus(boolean status) {
		this.status = status;
	}

	@ManyToMany(fetch = FetchType.EAGER)
	@Cascade(CascadeType.ALL)
	@JoinTable(name = "COMPANY_SUBSECTOR", joinColumns = { @JoinColumn(name = "COMPANY_ID") }, inverseJoinColumns = { @JoinColumn(name = "SUBSECTOR_ID") })
	public Set<SubSector> getSubSectors() {
		return subSectors;
	}

	public void setSubSectors(Set<SubSector> subSectors) {
		this.subSectors = subSectors;
	}

}
